package com.smallkingson.ylib.net.http.bean;

import com.smallkingson.ylib.db.cache.YKey;
import com.smallkingson.ylib.net.http.YHttps;

import java.util.HashMap;
import java.util.Map;


public class Request implements YKey {

	int requestCode;

	int page;

	String url;

	String cacheKey;
	/**
	 * 设置该请求的缓存时间
	 */
	long aliveSecond;

	Method method = Method.GET;

	/**
	 * 请求头
	 */
	Map<String, String> headers;
	/**
	 * post请求时 所用到的参数
	 */
	Map<String, String> params;

	private Map<String, Object> tags;

	private boolean requestNet;

	String postString;

	public Map<String, String> getParams() {
		return params;
	}

	public Request() {
		tags = new HashMap<String, Object>();
	}

	public Method getMethod() {
		return method;
	}

	/**
	 * post请求时 用此方法添加参数
	 * 
	 * @return
	 */
	public Map<String, String> p(String key, String value) {

		if (this.params == null) {
			this.params = new HashMap<String, String>();
		}
		this.params.put(key, value);
		return params;
	}

	public YHttps.YHttpListener listener;

	public static final int REQUEST_INIT = 0X9527;
	public static final int REQUEST_REFRESH = 0X9528;
	public static final int REQUEST_LOADMORE = 0X9529;

	public static Request newInstance(YHttps.YHttpListener listener) {
		Request request = new Request();
		request.listener = listener;
		return request;
	}

	public String getPostString() {
		return postString;

	}
	public Request setPostString(String postString) {
		this.postString = postString;
		return this;
	}
	public int getRequestCode() {
		return requestCode;
	}

	public Request setMethod(Method method) {

		this.method = method;
		return this;
	}

	public Request setRequestCode(int request) {
		this.requestCode = request;
		return this;
	}

	public int getPage() {
		return page;
	}

	public Request setPage(int page) {
		this.page = page;
		return this;
	}

	/**
	 * 设置了这个就无需再设置setUrl(String url)与setCacheKey(String cacheKey)了
	 * 
	 * @param url
	 */
	public Request yUrl(YUrl url) {
		this.url = url.getUrl();
		this.cacheKey = url.getNoAuthUrl();
		return this;
	}

	/**
	 * 添加请求头
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public Request h(String key, String value) {

		if (headers == null) {
			headers = new HashMap<String, String>();
		}
		headers.put(key, value);

		return this;
	}

	public String getUrl() {
		return url;
	}

	public Request setUrl(String url) {
		this.url = url;
		return this;
	}

	public String getCacheKey() {
		return cacheKey;
	}

	public Request setCacheKey(String cacheKey) {
		this.cacheKey = cacheKey;
		return this;
	}

	public long getAliveSecond() {
		return aliveSecond;
	}
	public long setReuqestNet() {
		return aliveSecond;
	}
	public Request setAliveSecond(long aliveSecond) {
		this.aliveSecond = aliveSecond;
		return this;
	}

	public Map<String, String> getHeaders() {
		return headers;
	}

	@Override
	public String toString() {
		return "Request [requestCode=" + requestCode + ", page=" + page
				+ ", url=" + url + ", cacheKey=" + cacheKey + ", aliveSecond="
				+ aliveSecond + ", listener=" + listener + "]";
	}

	public Object getTag(String key) {
		return tags.get(key);
	}

	public Request putTag(String key, Object value) {
		tags.put(key, value);
		return this;
	}
	public Request setRequestNet(boolean requestNet) {
		this.requestNet=requestNet;
		return this;
	}
	public boolean isRequestNet() {
		return requestNet;
	}

	public static enum Method {
		GET, POST
	}

	@Override
	public String getKey() {
		return getCacheKey();
	}

}
